package com.cqust.service;

import com.cqust.mapper.AttendanceMapper;
import com.cqust.pojo.Attendance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class AttendanceServiceImpl implements AttendanceService{
    @Autowired
    private AttendanceMapper attendanceMapper;

    public List<Attendance> getAttendances() {
        return attendanceMapper.getAttendances();
    }

    public void addAttendance(Attendance attendance) {
        attendanceMapper.addAttendance(attendance);
    }

    public void updateAttendance(String eid, String offDate) {
        Attendance attendance = attendanceMapper.getAttendance(eid);
        String workDate = attendance.getWorkDate();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        double h = 0;
        try {
            Date parse1 = sdf.parse(workDate);
            Date parse2 = sdf.parse(offDate);
            double diff = parse2.getTime() - parse1.getTime();
            h = diff / (1000 * 3600);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String working = String.format("%.1f", h)+"小时";
        attendanceMapper.updateAttendance(eid,offDate,working);
    }

    public Attendance getAttendance(String eid) {
        return attendanceMapper.getAttendance(eid);
    }

    public void deleteAttendance(String eid) {
        attendanceMapper.deleteAttendance(eid);
    }
}
